package com.py.custom.tms.application.atomic.hash;

import java.util.*;

/**
 * description：
 *
 * @author pengyou
 * @version 1.0.0
 * @date 2022/4/17
 */
public class HashTest {

    public static void main(String[] args) {
        String s = "anagram";
        String t = "nagaram";
        System.out.println("isAnagram(s, t) = " + isAnagram(s, t));
        int[] nums1 = {1, 2, 2, 1};
        int[] nums2 = {2, 2};
        System.out.println("intersection(nums1, nums2) = " + Arrays.toString(intersection(nums1, nums2)));

        System.out.println("isHappy(19) = " + isHappy(19));
        int[] nums = {2, 7, 11, 9};
        int target = 9;
        System.out.println("twoSum(nums, target) = " + Arrays.toString(twoSum(nums, target)));
    }


    /**
     * 给定两个字符串 s 和 t ，编写一个函数来判断 t 是否是 s 的字母异位词。
     *
     * @param s
     * @param t
     * @return
     */
    public static boolean isAnagram(String s, String t) {
        if (s == null && t == null) {
            return true;
        }
        if (s == null || t == null) {
            return false;
        }
        if (s.length() != t.length()) {
            return false;
        }

        int[] record = new int[26];
        for (char c : s.toCharArray()) {
            record[c - 'a'] += 1;
        }
        for (char c : t.toCharArray()) {
            record[c - 'a'] -= 1;
        }
        for (int i : record) {
            if (i != 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * 数组的交集
     *
     * @param nums1
     * @param nums2
     * @return
     */
    public static int[] intersection(int[] nums1, int[] nums2) {
        if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
            return new int[0];
        }
        Set<Integer> set = new HashSet<>();
        Set<Integer> resSet = new HashSet<>();

        for (int i = 0; i < nums1.length; i++) {
            set.add(nums1[i]);
        }

        for (int i = 0; i < nums2.length; i++) {
            if (set.contains(nums2[i])) {
                resSet.add(nums2[i]);
            }
        }
        int index = 0;
        int[] resArr = new int[resSet.size()];
        for (Integer num : resSet) {
            resArr[index++] = num;
        }
        return resArr;
    }

    /**
     * 开心数
     *
     * @param n
     * @return
     */
    public static boolean isHappy(int n) {
        Set<Integer> record = new HashSet<>();
        while (n != 1 && !record.contains(n)) {
            record.add(n);
            n = getNextNumber(n);
        }
        return n == 1;
    }

    private static int getNextNumber(int n) {
        int res = 0;
        while (n > 0) {
            int temp = n % 10;
            res += temp * temp;
            n /= 10;
        }
        return res;
    }

    /**
     * 两数之和
     *
     * @param nums
     * @param target
     * @return
     */
    public static int[] twoSum(int[] nums, int target) {
        int[] res = new int[2];
        if (nums == null || nums.length == 0) {
            return res;
        }
        Map<Integer, Integer> record = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            int temp = target - nums[i];
            if (record.containsKey(temp)) {
                res[1] = i;
                res[0] = record.get(temp);
                return res;
            }
            record.put(nums[i], i);
        }
        return res;
    }
}
